package com.feicheng.iou.service.impl.open;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.RepaymentConstant;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.*;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.mapper.*;
import com.feicheng.iou.pojo.login.LoginParamPOJO;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.PaymentService;
import com.feicheng.iou.service.impl.PaymentServiceImpl;
import com.feicheng.iou.service.student.open.StudentOpenService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName StudentOpenServiceImpl
 * @Description TODO
 * @Author luoxin
 * @Date 2021/8/31 1:12 下午
 */
@Service
@Slf4j
public class StudentOpenServiceImpl implements StudentOpenService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RelMapper relMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    PaymentMapper paymentMapper;
    @Autowired
    PaymentOptionsMapper paymentOptionsMapper;
    @Autowired
    ProjectServiceMapper projectServiceMapper;
    @Autowired
    PaymentItemMapper paymentItemMapper;


    @Override
    public ResultInfoV110 selectUserInfoOfLogin(LoginParamPOJO loginParamPOJO,HttpServletRequest request) {
        log.info("UserServiceImpl的selectUserInfoOfLogin方法进入，参数loginParamPOJO：" + loginParamPOJO);
        User user = userMapper.selectUserInfoOfLoginPhone(loginParamPOJO);
        //校验用户名和密码是否正确
        if (user == null) {
            throw new SystemException(ReturnCode.ACCOUNT_PASSWORD_WRONG);
        }
        boolean isFirst =false;
        if(user.getOpenId() ==null){
            System.out.println("~~~~~~~~~~~~~"+user.getOpenId());
            isFirst =true;
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("user",user);
        resultMap.put("isFirstLogin",isFirst);
        //封装session
        request.getSession().setAttribute(SessionConstant.STUDENT_CURRENT, user);
        return ResultInfoV110.success(resultMap);
    }

    @Override
    public ResultInfoV110 updatePassword(String password,Integer userId,String openId) {
        log.info("StudentOpenServiceImpl的updatePassword方法进入参数为密码："+password);

        //取到当前登录用户session中的userid
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("user_id", userId).set("password", password).set("open_id",openId);
        int update = userMapper.update(null, userUpdateWrapper);
        if (update == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "更改用户密码"));
        }

        log.info("StudentOpenServiceImpl的updatePassword方法结束");
        return ResultInfoV110.success();
    }

    @Override
    public ResultInfoV110 selectInfo(Integer userId) {
        log.info("StudentOpenServiceImpl的selectInfo方法进入");

        HashMap<String, Object> resultMap = new HashMap<>();

        //取到当前登录用户session中的userid
        User user = userMapper.selectById(userId);

        //根据用户id查询中间表信息
        QueryWrapper<Rel> relQueryWrapper = new QueryWrapper<>();
        relQueryWrapper.eq("user_id",userId);
        Rel rel = relMapper.selectOne(relQueryWrapper);
        Integer studentId = rel.getStudentId();

        //根据中间表中的学生Id查询学生信息
        Student student = studentMapper.selectById(studentId);

        //封装所有查询到的信息
        resultMap.put("student",student);
        resultMap.put("user",user);

        log.info("StudentOpenServiceImpl的selectInfo方法结束");
        return ResultInfoV110.success(resultMap);
    }

    @Override
    public ResultInfoV110 update(Student student, User user) {
        log.info("StudentOpenServiceImpl的update方法进入参数为学生信息："+student+"用户信息："+user);

        int updateStudent = studentMapper.updateById(student);
        if (updateStudent == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "更改学生信息"));
        }

        int updateUser = userMapper.updateById(user);
        if (updateUser == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "更改线录信息"));
        }

        log.info("StudentOpenServiceImpl的update方法结束");
        return ResultInfoV110.success();
    }

    @Override
    public ResultInfoV110 selectPayment(Integer userId) {
        log.info("StudentOpenServiceImpl的selectPayment方法进入");

        HashMap<String, Object> resultMap = new HashMap<>();

        QueryWrapper<Rel> relQueryWrapper = new QueryWrapper<>();
        relQueryWrapper.eq("user_id",userId);
        Rel rel = relMapper.selectOne(relQueryWrapper);

        Integer serviceId = rel.getServiceId();
        ProjectService projectService = projectServiceMapper.selectById(serviceId);

        Integer paymentId = rel.getPaymentId();
        Payment payment = paymentMapper.selectById(paymentId);

        Integer poid = payment.getPoid();
        PaymentOptions paymentOptions = paymentOptionsMapper.selectById(poid);

        QueryWrapper<PaymentItem> paymentItemQueryWrapper = new QueryWrapper<>();
        paymentItemQueryWrapper.eq("p_id",paymentId);
        List<PaymentItem> paymentItems = paymentItemMapper.selectList(paymentItemQueryWrapper);

        resultMap.put("projectService",projectService);
        resultMap.put("payment",payment);
        resultMap.put("paymentOptions",paymentOptions);
        resultMap.put("paymentItems",paymentItems);

        log.info("StudentOpenServiceImpl的selectPayment方法结束");
        return ResultInfoV110.success(resultMap);
    }

    @Override
    public ResultInfoV110 repayment(Integer pamentItemId, String businessTNumber) {
        log.info("StudentOpenServiceImpl的repayment方法进入,还款Id为"+pamentItemId+"账单流水号为"+businessTNumber);

        UpdateWrapper<PaymentItem> paymentItemUpdateWrapper = new UpdateWrapper<>();
        paymentItemUpdateWrapper.eq("id", pamentItemId).set("status", RepaymentConstant.YIHUANKUAN);
        int updateRepayment = paymentItemMapper.update(null, paymentItemUpdateWrapper);
        if (updateRepayment == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "还款失败"));
        }

        log.info("StudentOpenServiceImpl的repayment方法结束");
        return ResultInfoV110.success();
    }
}
